use serde::{Deserialize, Serialize};
use validator::Validate;

const DEFAULT_PAGE_SIZE: u64 = 10;
const DEFAULT_PAGE_NUMBER: u64 = 1;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
pub struct PaginationParams {
    #[validate(range(min = 1, message = "页码必须大于0"))]
    #[serde(
        default = "default_page_num",
        deserialize_with = "crate::common::serde::deserialize_number"
    )]
    pub page_num: u64,
    #[validate(range(min = 1, max = 100, message = "分页大小必须在0 ~ 100之间"))]
    #[serde(
        default = "default_page_size",
        deserialize_with = "crate::common::serde::deserialize_number"
    )]
    pub page_size: u64,
}

fn default_page_num() -> u64 {
    DEFAULT_PAGE_NUMBER
}
fn default_page_size() -> u64 {
    DEFAULT_PAGE_SIZE
}

#[derive(Serialize, Debug)]
pub struct PageList<T> {
    pub page_num: u64,
    pub page_size: u64,
    pub total: u64,
    pub items: Vec<T>,
}

impl<T> PageList<T> {
    pub fn new(page_num: u64, page_size: u64, total: u64, items: Vec<T>) -> Self <> {
        PageList {
            page_num,
            page_size,
            total,
            items,
        }
    }
    pub fn from_pagination(PaginationParams { page_num, page_size }: PaginationParams, total: u64, items: Vec<T>) -> Self {
        Self::new(page_num, page_size, total, items)
    }
}